package com.yicong.ants;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import com.cchao.simplelib.Const;
import com.cchao.simplelib.LibCore;
import com.cchao.simplelib.core.Router;
import com.cchao.simplelib.core.UiHelper;
import com.cchao.simplelib.ui.interfaces.BaseStateView;
import com.cchao.simplelib.ui.interfaces.BaseView;
import com.cchao.simplelib.ui.interfaces.impl.DefaultTitleBarDelegate;
import com.umeng.umcrash.UMCrash;
import com.yicong.ants.manager.AppHelper;
import com.yicong.ants.manager.UserManager;
import com.yicong.ants.manager.http.OkHttpManager;
import com.yicong.ants.mvp.libconfig.BaseViewDelegate;
import com.yicong.ants.mvp.libconfig.ZsStateViewDelegate;
import com.yicong.ants.mvp.libconfig.ZsTitleBarDelegate;
import com.yicong.ants.utils.ConstantCodeUtils;

import java.util.Map;

import okhttp3.OkHttpClient;

public class LibCoreSupport implements LibCore.InfoSupport {
    @Override
    public OkHttpClient getOkHttpClient() {
        return OkHttpManager.getOriginClient();
    }

    @Override
    public boolean isDebug() {
        return AppHelper.isDebug();
    }

    @Override
    public String getAppName() {
        return LibCore.getContext().getString(R.string.app_name);
    }

    @Override
    public String getAppId() {
        return BuildConfig.APPLICATION_ID;
    }

    @Override
    public int getAppVersionCode() {
        return BuildConfig.VERSION_CODE;
    }

    @Override
    public Map<String, String> getWebParams() {
        return OkHttpManager.getCommonParams();
    }

    @Override
    public LibCore.RouterConfig getRouterConfig() {
        return new LibCore.RouterConfig() {
            @Override
            public boolean checkLogin(Context context, boolean isNeedLogin) {
                boolean hasLogin = UserManager.isLogin();
                if (isNeedLogin && !hasLogin) {
                    UserManager.routeLoginResult(context, ConstantCodeUtils.START_LOGIN);
                }
                return hasLogin;
            }

            @Override
            public boolean checkStatus(Context context, String statusName, boolean isNeedObtainStatus) {
                switch (statusName) {
                    case Constants.Rout.bind_card:
                        // 先检测登录
                        boolean hasLogin = checkLogin(context, true);
                        if (!hasLogin) {
                            return false;
                        }
                        boolean hasBind = UserManager.getUserBean().getCard_type() != 0;
                        return hasBind;
                }
                return false;
            }
        };
    }

    @Override
    public LibCore.ILogEvents getLogEvents() {
        return new LibCore.ILogEvents() {
            @Override
            public void logEvent(String tag, String event) {
//                BuglyLog.i(tag, event);
                switch (tag) {
                    case Const.LogTag.Activity_On_Created:
                        break;
                }
            }

            @Override
            public void logException(Throwable e) {
                if (AppHelper.isDebug()) {
                    UiHelper.showLongToast(e.getMessage());
                } else {
                    UiHelper.showLongToast(UiHelper.getString(R.string.net_error));
                }
                UMCrash.generateCustomLog(e,"logException");
            }
        };
    }

    // 配置项
    public static class LibConfigImpl extends LibCore.LibConfig {
        private static LibCore.LibConfig mInstance;

        public static LibCore.LibConfig get() {
            if (mInstance == null) {
                mInstance = new LibConfigImpl();
            }
            return mInstance;
        }

        @Override
        public BaseView getBaseViewDelegate(Context context) {
            return new BaseViewDelegate(context);
        }

        @Override
        public BaseStateView getStateViewDelegate(Context context, View childContent, Runnable retryCallBack) {
            return new ZsStateViewDelegate(context, childContent, retryCallBack);
        }

        @Override
        public DefaultTitleBarDelegate getTitleBarDelegate(Context context, ViewGroup parent) {
            return new ZsTitleBarDelegate(context, parent);
        }
    }
}
